package sskj.regulatory.presentation.screen.signature.signedlist

import android.content.res.Resources
import android.graphics.Color
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter
import com.qmuiteam.qmui.widget.QMUIEmptyView
import com.vondear.rxtools.view.RxToast
import kotlinx.android.synthetic.main.fragment_home.view.*
import kotlinx.android.synthetic.main.fragment_signed_list.view.*
import kotlinx.android.synthetic.main.fragment_sitevideo_list.view.*
import sskj.regulatory.R
import sskj.regulatory.domain.entity.sitevideo.Site
import sskj.regulatory.presentation.adapter.SignedRecyclerArrayAdapter
import sskj.regulatory.presentation.adapter.SiteVideoRecyclerArrayAdapter
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.decorator.GridDividerItemDecoration
import javax.inject.Inject
import com.vondear.rxtools.RxImageTool.dip2px
import com.jude.easyrecyclerview.decoration.DividerDecoration
import sskj.regulatory.presentation.screen.sitevideo.siteveiw.SiteViewFragment
import android.support.v7.widget.StaggeredGridLayoutManager
import android.util.DisplayMetrics
import com.jude.easyrecyclerview.decoration.SpaceDecoration
import com.qmuiteam.qmui.util.QMUIDisplayHelper


class SiteVideoListFragment : BaseFragment<SiteVideoListContract.View, SiteVideoListContract.Presenter>(), SiteVideoListContract.View {

    @Inject
    lateinit var mPresenter: SiteVideoListPresenter

    private val mAdapter: SiteVideoRecyclerArrayAdapter = SiteVideoRecyclerArrayAdapter(context)

    override fun initPresenter() = mPresenter

    override fun injectDependencies() = activityComponent.inject(this)

    override fun onBackPressed(): Boolean = true

    override fun translucentFull() = true

    override val layoutResId: Int = R.layout.fragment_sitevideo_list

    override fun initView() {
        initRecyclerView()
        mPresenter.initData()
    }


    private fun initRecyclerView() {

        mCacheView.svEasyRecyclerView.emptyView.findViewById<QMUIEmptyView>(R.id.emptyView).setButton("点击重试",
                { mPresenter.reLoad() })
        var gridLayoutManager = GridLayoutManager(context, 2)
        gridLayoutManager.spanSizeLookup = mAdapter.obtainGridSpanSizeLookUp(2)
        mCacheView.svEasyRecyclerView.setLayoutManager(gridLayoutManager)
        var itemDecoration = SpaceDecoration(QMUIDisplayHelper.dp2px(context, 10));//参数是距离宽度
        itemDecoration.setPaddingEdgeSide(true);//是否为左右2边添加padding.默认true.
        itemDecoration.setPaddingStart(true);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration.setPaddingHeaderFooter(true);//是否对Header于Footer有效,默认false.
        mCacheView.svEasyRecyclerView.addItemDecoration(itemDecoration);



        mCacheView.svEasyRecyclerView.setAdapterWithProgress(mAdapter)

        mCacheView.svEasyRecyclerView.setRefreshing(false)


        mCacheView.svEasyRecyclerView.setRefreshListener {
            mPresenter.reLoad()
        }

        //更多加载
        mAdapter.setMore(R.layout.view_more, object : RecyclerArrayAdapter.OnMoreListener {

            override fun onMoreShow() {
                mPresenter.loadMore()
            }

            override fun onMoreClick() {

            }
        })

        //没有更多
        mAdapter.setNoMore(R.layout.view_no_more)


        //点击事件
        mAdapter.setOnItemClickListener {

            var siteViewFragment: SiteViewFragment = SiteViewFragment()

            siteViewFragment.arguments = SiteViewFragment.getBundle(mAdapter.getItem(it))

            startFragment(siteViewFragment)

        }
    }


    override fun clearSites() {
        mAdapter.clear()
    }


    override fun onSiteListReceive(siteList: List<Site>) {
        mAdapter.addAll(siteList)
    }

    override fun showNoSites() {
        mAdapter.stopMore()
        mAdapter.notifyDataSetChanged()
    }

    override fun showEmpty() {
        mCacheView.svEasyRecyclerView.showEmpty()
    }

    override fun showError() {
        mCacheView.svEasyRecyclerView.showError()
    }

    override fun showInitLoading() {
        mCacheView.svEasyRecyclerView.showProgress()

    }
}